package com.lib.DB;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.lib.model.wordInfo;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

public class DBHelper {
	/**
	 * 得到数据库中不同条目的个数
	 * @return
	 * @throws SQLException
	 */
	public static int getCount() throws SQLException
	{
		String sql="select count(*) from wordinfo";
		Connection connection=null;
		PreparedStatement pstmst=null;
		ResultSet rst=null;
		int count=0;
		try {
			connection=(Connection) DBUtil.getConnection();
			pstmst=(PreparedStatement) connection.prepareStatement(sql);
		    rst=pstmst.executeQuery();
		    rst.next();
		    count=rst.getInt(1);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
			DBUtil.CloseConn(connection, pstmst,rst);
		}
		return count;
	}
	/**
	 * 得到不同关键字个数
	 * @return
	 * @throws SQLException
	 */
	public static int getDistinctWord() throws SQLException
	{
		String sql="select count(distinct(word)) from wordInfo";
		Connection connection=null;
		PreparedStatement pstmst=null;
		ResultSet rst=null;
		int count=0;
		try {
			connection=(Connection) DBUtil.getConnection();
			pstmst=(PreparedStatement) connection.prepareStatement(sql);
		    rst=pstmst.executeQuery();
		    rst.next();
		    count=rst.getInt(1);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
			DBUtil.CloseConn(connection, pstmst,rst);
		}
		return count;
	}
	/**
	 * 得到所有的关键字
	 * @return
	 */
	public static List<String> getAllDistinctWord()
	{
		List<String> distinctWordList=new ArrayList<String>();
		String sql="select distinct(word) from wordInfo";
		Connection connection=null;
		PreparedStatement pstmst=null;
		ResultSet rst=null;
		int count=0;
		try {
			connection=(Connection) DBUtil.getConnection();
			pstmst=(PreparedStatement) connection.prepareStatement(sql);
		    rst=pstmst.executeQuery();
		    while(rst.next())
		    {
		    	String tempWord=rst.getString(1);
		    	distinctWordList.add(tempWord);
		    }
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
			try {
				DBUtil.CloseConn(connection, pstmst,rst);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return distinctWordList;
	}
	/**
	 * 得到文件fileName的tf值
	 * @param fileName
	 * @return
	 */
	public static Map<String,Double> getWordAndTF(String fileName)
	{
		Map<String, Double> wordTFMap=new HashMap<String, Double>();
		String sql="select word,tf from wordInfo where fileName=?";
		Connection connection=null;
		PreparedStatement pstmst=null;
		ResultSet rst=null;
		int count=0;
		try {
			connection=(Connection) DBUtil.getConnection();
			pstmst=(PreparedStatement) connection.prepareStatement(sql);
			pstmst.setString(1, fileName);
		    rst=pstmst.executeQuery();
		    while(rst.next())
		    {
		    	String word=rst.getString(1);
		    	double tf=rst.getDouble(2);
		    	wordTFMap.put(word, tf);
		    }
		   
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
			try {
				DBUtil.CloseConn(connection, pstmst,rst);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return wordTFMap;
	}
	public static Map<String, Double>getIDFSForWords(List<String> wordList)
	{
		Map<String, Double>idfsMap=new HashMap<String, Double>();
		Iterator< String>iterator=wordList.iterator();
		while(iterator.hasNext())
		{
			String word=iterator.next();
			double idf=getIDFForWord(word);
			idfsMap.put(word, idf);
		}
		return idfsMap;
	}
	/**
	 * 得到关键字列表
	 * @param fileName
	 * @return
	 */
	public static List<String> getWordList(String fileName)
	{
		List<String>wordlList=new ArrayList<String>();
		String sql="select word from wordInfo where fileName=?";
		Connection connection=null;
		PreparedStatement pstmst=null;
		ResultSet rst=null;
		int count=0;
		try {
			connection=(Connection) DBUtil.getConnection();
			pstmst=(PreparedStatement) connection.prepareStatement(sql);
			pstmst.setString(1, fileName);
		    rst=pstmst.executeQuery();
		    while(rst.next())
		    {
		    	String word=rst.getString(1);
		    	wordlList.add(word);
		    }
		   
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
			try {
				DBUtil.CloseConn(connection, pstmst,rst);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return wordlList;
	}
	/**
	 * 得到关键字的idf值
	 * @param word
	 * @return
	 */
	public static double getIDFForWord(String word)
	{
		String sql="select distinct(idf) from wordInfo where word=?";
		Connection connection=null;
		PreparedStatement pstmst=null;
		ResultSet rst=null;
		double idf=0;
		try {
			connection=(Connection) DBUtil.getConnection();
			pstmst=(PreparedStatement) connection.prepareStatement(sql);
			pstmst.setString(1, word);
			rst=pstmst.executeQuery();
			rst.next();
			idf=rst.getInt(1);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally
			{
				try {
					DBUtil.CloseConn(connection, pstmst,rst);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		return idf;
	
	}
	/**
	 * 得到所有的文件名
	 * @return
	 */
	public static List<String>getAllFileName()
	{
		List<String>resultList=new ArrayList<String>();
		String sql="select distinct(fileName) from wordInfo";
		Connection conn=null;
		PreparedStatement pstm=null;
		ResultSet rst=null;
		try {
			conn=(Connection) DBUtil.getConnection();
			pstm=(PreparedStatement) conn.prepareStatement(sql);
			rst=pstm.executeQuery();
			while(rst.next())
			{
				String fileName=rst.getString(1);
				resultList.add(fileName);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
			try {
				DBUtil.CloseConn(conn, pstm,rst);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return resultList;
	}
	/**
	 * 判断某个关键字是否存在
	 * @param word
	 * @return
	 */
	public static boolean isWordExists(String word)
	{
		boolean flag=false;
		String sql="select count(*) from wordInfo where word=?";
		Connection conn=null;
		PreparedStatement pstmst=null;
		ResultSet rs=null;
		try {
			conn=(Connection) DBUtil.getConnection();
			pstmst=(PreparedStatement)conn.prepareStatement(sql);
			pstmst.setString(1, word);
			rs=pstmst.executeQuery();
			rs.next();
			if(rs.getInt(1)>0)
			{
				flag=true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
			try {
				DBUtil.CloseConn(conn, pstmst,rs);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return flag;
	}
}